<?php
require_once 'config.php';
require_once 'startup.php'; 

require_once 'json.php';

$status = '';

if(($request->server['REQUEST_METHOD'] == 'POST') && isset($request->post['email']) && isset($request->post['method'])){
    $pattern = '/^[A-Z0-9._%-+]+@[A-Z0-9][A-Z0-9.-]{0,61}[A-Z0-9]\.[A-Z]{2,6}$/i';           
    if(preg_match($pattern, $request->post['email'])) {
        $row = $db->query("SELECT * FROM " . DB_PREFIX . "newsletter WHERE email = '" . $db->escape($request->post['email']) . "'");
        
        if($request->post['method'] == 'subscriber'){                    
            if($row->num_rows){           
                $status = 'E-mail "' . $request->post['email'] . '", som allerede eksisterer i vores system.';    
                $type = '0';
            }
            else{
                try{
                    $db->query("INSERT INTO " . DB_PREFIX . "newsletter SET email = '" . $db->escape($request->post['email']) . "', date_added = NOW()");
                                            
                    $status = 'E-mail "' . $request->post['email'] . '" er blevet registreret en succes.';                                                
                    $type = '1';
                }   
                catch(Exception $e){
                    $status = 'Fejl under behandlingen';    
                }
                
                $subject = 'Registrer abonnent';
                $message = 'Kære!<br />Din e-mail har været en succes tilføjet til vores abonnent system.<br /><br />';                    
                //sendMail($request->post['email'], $subject, $message);                      
            }
        }
        elseif($request->post['method'] == 'unsubscriber'){ 
            if($row->num_rows){           
                try{
                    $db->query("DELETE FROM " . DB_PREFIX . "newsletter WHERE email = '" . $db->escape($request->post['email']) . "'");
                    
                    $status = 'E-mail "' . $request->post['email'] . '" er blevet fjernet fra vores system.';
                    $type = '1';
                }
                catch(Exception $e){
                    $status = 'Fejl under behandlingen.';    
                }
                
                $subject = 'Fjern Abonnent';
                $message = 'Kære!<br />Din e-mail er blevet en succes fjernet fra vores abonnent system.<br /><br />';                                
                //sendMail($request->post['email'], $subject, $message);
            }
            else{
                $status = 'E-mail "' . $request->post['email'] . '" eksisterer ikke i vores system.';   
                $type = '0';
            }        
        }
    }
    else{
        $status = 'Ugyldige data, der sendes.';
        $type = '0';
    }
}

$data = array(
    'type'     => $type,
    'status'   => $status, 
);    

echo Json::encode($data);        

/* Send mail function */
function sendMail($to, $subject, $message){  
    global $config;             
    $subject = $config['system_name'] . ' - ' . $subject;
    
    $message .= '<b><a href="' . $config['url_home'] . '">' . $config['name'] . '</a></b><br />';        
    $message .= str_replace("\n", '<br />', $config['address']) . '<br />';
    $message .= 'Tel: ' . $config['phone'] . '<br />';
    $message .= 'Fax: '  . $config['fax'] . '<br />';
    $message .= 'Email: ' . $config['email'];
                
    $mail = new Mail();
    $mail->protocol = $config['mail_protocol'];
    $mail->parameter = $config['mail_parameter'];
    $mail->hostname = $config['smtp_host'];
    $mail->username = $config['smtp_username'];
    $mail->password = $config['smtp_password'];
    $mail->port = $config['smtp_port'];
    $mail->timeout = $config['smtp_timeout'];   
                                  
    $mail->setFrom($config['email']);             
    $mail->setTo($to);        
    $mail->setSender($config['system_name']);        
    $mail->setSubject($subject);
    $mail->setHtml($message);
    $mail->send();
}
?>     